package cn.qiyu5522.tk_ems.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.qiyu5522.tk_ems.interceptor.TraceIdRequestLoggingInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: tk_ems
 * @BelongsPackage: cn.qiyu5522.tk_ems.config
 * @Author: TangQi
 * @CreateTime: 2023-02-01  10:18
 * @Description: TODO
 * @Version: 1.0
 */

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Autowired
    public void rewriteSaStrategy() {
        // 重写Sa-Token的注解处理器，增加注解合并功能
        SaStrategy.me.getAnnotation = (element, annotationClass) -> {
            return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
        };
    }

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 日志链路追踪
        registry.addInterceptor(new TraceIdRequestLoggingInterceptor()).order(1);

        
        // 注册 Sa-Token 拦截器，定义详细认证规则
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 指定一条 match 规则
            SaRouter
                    .match("/**")    // 拦截的 path 列表，可以写多个 */
                    .notMatch(
                            "/admin/login/*",
                            "/admin/student/importStudentDataTemplate",
                            "/admin/student/exportStudentData",
                            "/api/*",
                            "/swagger-resources/**",
                            "/webjars/**",
                            "/v2/**",
                            "/swagger-ui.html/**",
                            "/doc.html/**",
                            "/error",
                            "/favicon.ico",
                            "/**/*.jpg",
                            "/**/*.png",
                            "/**/*.jpeg")        // 排除掉的 path 列表，可以写多个
                    .check(r -> StpUtil.checkLogin());        // 要执行的校验动作，可以写完整的 lambda 表达式

            // 根据路由划分模块，不同模块不同鉴权
//            SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
//            SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
//            SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
//            SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
//            SaRouter.match("/notice/**", r -> StpUtil.checkPermission("notice"));
//            SaRouter.match("/comment/**", r -> StpUtil.checkPermission("comment"));
        }))
                .addPathPatterns("/**");

;


        // 完成后开放
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
//        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()).isAnnotation(true))
//                .addPathPatterns("/**")
//                .excludePathPatterns("/admin/login/*","**/api/**",
//                        "/swagger-resources/**", "/webjars/**",
//                        "/v2/**", "/swagger-ui.html/**",
//                        "/doc.html/**","/error","/favicon.ico").order(2);

    }
}
